package com.dayuanit.atm.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dayuanit.atm.holder.WxAccessTokenHolder;
import com.dayuanit.atm.holder.WxUserInfoHolder;
import com.dayuanit.atm.util.ApiConnector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class WxAuthenHandler {

    @Value("${wx.code.url}")
    private String codeUrl;

    @Value("${wx.appid}")
    private String appId;

    @Value("${wx.redirect.uri}")
    private String redirectUrl;

    @Value("${wx.response.type}")
    private String responseType;

    @Value("${wx.scope}")
    private String scope;

    @Value("${wx.access.token.url}")
    private String accessTokenUrl;

    @Value("${wx.secret}")
    private String secret;

    @Value("${wx.grant.type}")
    private String grantType;

    @Value("${wx.user.info.url}")
    private String userInfoUrl;


    /**
     * 生成访问二维码页面的请求地址
     * @return
     */
    public String getCodeUrl() {

        StringBuilder sx = new StringBuilder();
        sx.append(codeUrl)
                .append("?")
                .append("appid=").append(appId)
                .append("&")
                .append("redirect_uri=").append(redirectUrl)
                .append("&")
                .append("response_type=").append(responseType)
                .append("&")
                .append("scope=").append(scope);

        System.out.println("codeURl=" + sx.toString());
        return sx.toString();
    }

    /**
     * 获取微信AccessToken
     */
    public WxAccessTokenHolder getAccessToken(String code) {
        StringBuilder sx = new StringBuilder();
        sx.append(accessTokenUrl).append("?")
                .append("appid=").append(appId)
                .append("&")
                .append("secret=").append(secret)
                .append("&")
                .append("code=").append(code)
                .append("&")
                .append("grant_type=").append(grantType);

        // 发送请求获取access_token
        String msg = ApiConnector.get(sx.toString(), null);
        System.out.println("msg=" + msg);

        WxAccessTokenHolder wxAccessTokenHolder = JSON.parseObject(msg, WxAccessTokenHolder.class);

        return wxAccessTokenHolder;
    }

    /**
     * 微信用户的个人信息
     */
    public WxUserInfoHolder getWxUserInfo(WxAccessTokenHolder wxAccessTokenHolder) {

        StringBuilder sx = new StringBuilder();
        sx.append(userInfoUrl).append("?")
                .append("access_token=").append(wxAccessTokenHolder.getAccess_token())
                .append("&")
                .append("openid=").append(wxAccessTokenHolder.getOpenid());
        System.out.println("userInfoURL=" + sx.toString());
        String msg = ApiConnector.get(sx.toString(), null);
        System.out.println(">>>userInfo=" + msg);
        WxUserInfoHolder wxUserInfoHolder = JSON.parseObject(msg, WxUserInfoHolder.class);
        return wxUserInfoHolder;
    }
}
